import requests
import os
import re
def ref(str):
    head1={'Referer': 'https://img.ivsky.com/img/tupian/pre/'+str,'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36'}
    return head1
head={'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36'}
url1text=requests.get('https://www.ivsky.com/tupian/ziranfengguang/',headers=head).text
url1s=re.findall('</a></div><p><a href="(.*?)" title=".*?" target="_blank">.*?</a></p></li><li><div class="il_img">',url1text)
for url2 in url1s:
    url2text=requests.get("https://www.ivsky.com"+url2,headers=head).text
    dir_name=''.join(re.findall('<html><head><meta charset=utf-8 /><title>(.*?图片)',url2text))
    os.makedirs('D:\pics\\' + dir_name)
    url3s=re.findall('</a></div><p><a href="(.*?)"  title=".*?" target="_blank"',url2text)

    for url3 in url3s:
        url3text=requests.get('https://www.ivsky.com'+url3,headers=head).text
        pics=''.join(re.findall('img id="imgis" src=\'//img.ivsky.com/img/tupian/pre/(.*?)\' alt=".*?" /',url3text))
        file_name=pics.split('/')[-1]
        pic=requests.get('https://img.ivsky.com/img/tupian/pic/'+pics,headers=ref(pics))
        with open('D:\pics\\' + dir_name +'\\'+ file_name, 'wb') as f:
            f.write(pic.content)


